unit MailZH;
 
interface
uses
  Uni,DbHelper,IdPOP3,Vcl.ExtCtrls,IdMessage,System.Generics.Collections,classes,IdComponent,MGlobal
  ,receiveprogress,uitypes,Pop3Thread;
type
  PMailZH=^TMailZH;
  TMailZH=class
  private
    FPop3Ssl: integer;
    FSmtpPort: integer;
    FPop3password: string;
    FEmail: string;
    FZHLX: string;
    FMailBak: integer;
    FSmtpSsl: integer;
    FSmtpPassword: string;
    FPop3Server: string;
    FPop3Name: string;
    FSmtpServer: string;
    FCaption: string;
    FDbPath: string;
    FSmtpName: string;
    FSendName: string;
    FSmtpSamePop3: boolean;
    FPop3timer: integer;
    FReplyEMail: string;
    FSmtpAuth: boolean;
    FPop3Port: integer;
    FZHID: integer;
    FIsActive: Boolean;
    FDB: TDbHelper;
    tharr:TArray<Boolean>;
    UdlList:TList<string>;
    FPopTimer: TTimer;
    FPop: TIdPOP3;
    fth:tpop3thread;
    frmprogress:Tfrmreceiveprogress;
    FOnReceiveMsg: TReceiveMsgEvent;
    FOnReceiveProgress: TReceiveMsgProgressEvent;
    FOnReceiveRaw: TReceiveRawEvent;
    procedure SetCaption(const Value: string);
    procedure SetDbPath(const Value: string);
    procedure SetEmail(const Value: string);
    procedure SetMailBak(const Value: integer);
    procedure SetPop3Name(const Value: string);
    procedure SetPop3password(const Value: string);
    procedure SetPop3Port(const Value: integer);
    procedure SetPop3Server(const Value: string);
    procedure SetPop3Ssl(const Value: integer);
    procedure SetPop3timer(const Value: integer);
    procedure SetReplyEMail(const Value: string);
    procedure SetSendName(const Value: string);
    procedure SetSmtpAuth(const Value: boolean);
    procedure SetSmtpName(const Value: string);
    procedure SetSmtpPassword(const Value: string);
    procedure SetSmtpPort(const Value: integer);
    procedure SetSmtpSamePop3(const Value: boolean);
    procedure SetSmtpServer(const Value: string);
    procedure SetSmtpSsl(const Value: integer);
    procedure SetZHID(const Value: integer);
    procedure SetZHLX(const Value: string);
    procedure SetIsActive(const Value: Boolean);
    procedure SetDB(const Value: TDbHelper);

    
    procedure SetOnReceiveMsg(const Value: TReceiveMsgEvent);
    procedure SetOnReceiveProgress(const Value: TReceiveMsgProgressEvent);
    procedure SetOnReceiveRaw(const Value: TReceiveRawEvent);
    procedure PopWork(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCount: Int64);
    procedure PopWorkBegin(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCountMax: Int64);
  public
    msgindex,msgcount,index:integer;
    msgsize:Int64;
    curidmsg:tidmessage;
    constructor Create(query: TUniQuery);
    destructor Destory;
    property ZHID:integer read FZHID write SetZHID;
    property Caption:string read FCaption write SetCaption;
    property Email:string read FEmail write SetEmail;
    property SendName:string read FSendName write SetSendName;
    property ZHLX:string read FZHLX write SetZHLX;
    property Pop3Name:string read FPop3Name write SetPop3Name;
    property Pop3password:string read FPop3password write SetPop3password;
    property Pop3Server:string read FPop3Server write SetPop3Server;
    property Pop3timer:integer read FPop3timer write SetPop3timer;
    property Pop3Port:integer read FPop3Port write SetPop3Port;
    property Pop3Ssl:integer read FPop3Ssl write SetPop3Ssl;
    property SmtpName:string read FSmtpName write SetSmtpName;
    property SmtpPassword:string read FSmtpPassword write SetSmtpPassword;
    property SmtpServer:string read FSmtpServer write SetSmtpServer;
    property SmtpPort:integer read FSmtpPort write SetSmtpPort;
    property SmtpSsl:integer read FSmtpSsl write SetSmtpSsl;
    property SmtpAuth:boolean read FSmtpAuth write SetSmtpAuth;
    property SmtpSamePop3:boolean read FSmtpSamePop3 write SetSmtpSamePop3;
    property MailBak:integer read FMailBak write SetMailBak;
    property ReplyEMail:string read FReplyEMail write SetReplyEMail;
    property DbPath:string read FDbPath write SetDbPath;
    property IsActive:Boolean read FIsActive write SetIsActive;
    property DB:TDbHelper read FDB write SetDB;
    property Pop:TIdPOP3 read fpop;
    property TH:TPop3Thread read fth;
    property PopTimer:TTimer read fpoptimer;
    property OnReceiveMsg:TReceiveMsgEvent read FOnReceiveMsg write SetOnReceiveMsg;
    property  OnReceiveRaw:TReceiveRawEvent read FOnReceiveRaw write SetOnReceiveRaw;
    property OnReceiveProgress:TReceiveMsgProgressEvent read FOnReceiveProgress write SetOnReceiveProgress;
    procedure ReceiveEnd(index:integer);
    procedure ReceiveMsg(zh:string;var idmsg:TIdMessage);
    procedure ReceiveRaw(zh:string;uid:string;stream:TStream);

    procedure AddUdl(udl:string);
    procedure OnPopTimer(sender:TObject);

     procedure rpfDestroy(Sender: TObject);
    procedure Terminate(Sender: TObject);
    procedure InitTH;
    procedure StartTimer;
  end;

implementation
uses
  main,sysutils;

{ TMailZH }

procedure TMailZH.AddUdl(udl: string);
begin
  if not UdlList.Contains(udl) then
      UdlList.Add(udl);
end;

constructor TMailZH.Create(query: TUniQuery);
var
  ps:pstring;
begin
  if not query.eof then
  begin
    with query do
    begin
      zhid:=FieldByName('nid').AsInteger;
      caption:=fieldbyname('caption').AsString;
      email:=fieldbyname('email').AsString;
      sendname:=fieldbyname('sendname').AsString;
      zhlx:=fieldbyname('zhlx').AsString;
      pop3name:=fieldbyname('pop3name').AsString;
      pop3password:=fieldbyname('pop3password').AsString;
      pop3server:=fieldbyname('pop3server').AsString;
      pop3timer:=fieldbyname('pop3timer').AsInteger;
      pop3port:=fieldbyname('pop3port').AsInteger;
      pop3ssl:=fieldbyname('pop3ssl').AsInteger;
      smtpname:=fieldbyname('smtpname').AsString;
      smtppassword:=fieldbyname('smtppassword').AsString;
      smtpserver:=fieldbyname('smtpserver').AsString;
      smtpport:=fieldbyname('smtpport').AsInteger;
      smtpssl:=fieldbyname('smtpssl').AsInteger;
      smtpauth:=fieldbyname('smtpauth').AsBoolean;
      smtpsamepop3:=fieldbyname('smtpsamepop3').AsBoolean;
      mailbak:=fieldbyname('mailbak').AsInteger;
      replyemail:=fieldbyname('replyemail').AsString;
      dbpath:=fieldbyname('dbpath').AsString;
      fdb:=TDbHelper.Create(DbPath,query.Owner);
      UdlList:=tlist<string>.create;
       fth:=TPop3Thread.Create(email);
       fpop:=TIdPOP3.Create();
       new(ps);
       ps^:=email;
       FPop.Tag:=integer(ps);
    fpop.Host:=Pop3Server;
    FPop.Username:=Pop3Name;
    FPop.Password:=Pop3password;
    fpop.OnWork:=PopWork;
    fpop.OnWorkBegin:=PopWorkBegin;

    FPopTimer:=TTimer.Create(FPop);
    FPopTimer.Interval:=Pop3timer*60*1000;


  FPopTimer.OnTimer:=OnPopTimer;
  FPopTimer.Enabled:=true;
    end;
  end;
end;

destructor TMailZH.Destory;
begin
  db.Destory;
  UdlList.Free;
  Dispose(pstring(fpop.Tag));
  FPop.Free;
  FPopTimer.Free;
  inherited;
end;


procedure TMailZH.InitTH;
begin
  //if (not Assigned(fth)) then
      fth:=TPop3Thread.Create(email);
end;

procedure TMailZH.OnPopTimer(sender: TObject);
begin

      if (not Assigned(fth)) then
      fth:=TPop3Thread.Create(email);


      fth.initpop(fpop,UdlList);

      fth.OnTerminate:=Terminate;
      try
         fth.Start;
      except


      end;

  PopTimer.Enabled:=false;


end;

procedure TMailZH.PopWork(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Int64);
begin



end;

procedure TMailZH.PopWorkBegin(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCountMax: Int64);

begin

end;

procedure TMailZH.ReceiveEnd(index: integer);
begin

end;

procedure TMailZH.ReceiveMsg(zh:string;var idmsg:TIdMessage);
begin

end;

procedure TMailZH.ReceiveRaw(zh, uid: string; stream: TStream);
begin

end;

procedure TMailZH.rpfDestroy(Sender: TObject);
begin
  
end;

procedure TMailZH.SetCaption(const Value: string);
begin
  FCaption := Value;
end;

procedure TMailZH.SetDB(const Value: TDbHelper);
begin
  FDB := Value;
end;

procedure TMailZH.SetDbPath(const Value: string);
begin
  FDbPath := Value;
end;

procedure TMailZH.SetEmail(const Value: string);
begin
  FEmail := Value;
end;

procedure TMailZH.SetIsActive(const Value: Boolean);
begin
  FIsActive := Value;
end;

procedure TMailZH.SetMailBak(const Value: integer);
begin
  FMailBak := Value;
end;



procedure TMailZH.SetOnReceiveMsg(const Value: TReceiveMsgEvent);
begin
  FOnReceiveMsg := Value;
end;

procedure TMailZH.SetOnReceiveProgress(const Value: TReceiveMsgProgressEvent);
begin
  FOnReceiveProgress := Value;
end;

procedure TMailZH.SetOnReceiveRaw(const Value: TReceiveRawEvent);
begin
  FOnReceiveRaw := Value;
end;

procedure TMailZH.SetPop3Name(const Value: string);
begin
  FPop3Name := Value;
end;

procedure TMailZH.SetPop3password(const Value: string);
begin
  FPop3password := Value;
end;

procedure TMailZH.SetPop3Port(const Value: integer);
begin
  FPop3Port := Value;
end;

procedure TMailZH.SetPop3Server(const Value: string);
begin
  FPop3Server := Value;
end;

procedure TMailZH.SetPop3Ssl(const Value: integer);
begin
  FPop3Ssl := Value;
end;

procedure TMailZH.SetPop3timer(const Value: integer);
begin
  FPop3timer := Value;
end;


procedure TMailZH.SetReplyEMail(const Value: string);
begin
  FReplyEMail := Value;
end;

procedure TMailZH.SetSendName(const Value: string);
begin
  FSendName := Value;
end;

procedure TMailZH.SetSmtpAuth(const Value: boolean);
begin
  FSmtpAuth := Value;
end;

procedure TMailZH.SetSmtpName(const Value: string);
begin
  FSmtpName := Value;
end;

procedure TMailZH.SetSmtpPassword(const Value: string);
begin
  FSmtpPassword := Value;
end;

procedure TMailZH.SetSmtpPort(const Value: integer);
begin
  FSmtpPort := Value;
end;

procedure TMailZH.SetSmtpSamePop3(const Value: boolean);
begin
  FSmtpSamePop3 := Value;
end;

procedure TMailZH.SetSmtpServer(const Value: string);
begin
  FSmtpServer := Value;
end;

procedure TMailZH.SetSmtpSsl(const Value: integer);
begin
  FSmtpSsl := Value;
end;

procedure TMailZH.SetZHID(const Value: integer);
begin
  FZHID := Value;
end;

procedure TMailZH.SetZHLX(const Value: string);
begin
  FZHLX := Value;
end;

procedure TMailZH.StartTimer;
begin
  PopTimer.Enabled:=true;
end;

procedure TMailZH.Terminate(sender:TObject);
begin
  PopTimer.Enabled:=true;
  sender:=nil;
end;

end.
